package com.news.tools;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DoString {

    /**
     * 过滤危险字符
     */
    public static String filterStr(String str) {
        if (str == null) {
            str = "";
        } else {
            str = str.replaceAll("\\s", "");
            str = str.replaceAll(";", "");
            str = str.replaceAll("&", "");
            str = str.replaceAll("<", "");
            str = str.replaceAll(">", "");
            str = str.replaceAll("'", "");
            str = str.replaceAll("-", "");
            str = str.replaceAll("/", "");
            str = str.replaceAll("%", "");
        }
        return str;
    }

    /**
     * 设置字符编码格式
     */
    public static String toGBK(String str) {
        if (str == null) {
            str = "";
        }
        try {
            str = new String(str.getBytes(), "utf-8");
        } catch (Exception e) {
            //e.printStackTrace();
            str = "";
        }
        str = str.trim();
        return str;
    }

    /**
     * 把日期型数据转换为字符串
     */
    public static String formatDate(Date date) {
        String time = "";
        if (date == null) {
            date = new Date();
        }
        SimpleDateFormat hFormat = new SimpleDateFormat("yyyy-MM-dd");
        time = hFormat.format(date);
        return time;
    }

    /**
     * 把日期型数据转换为带有星期的日期字符串
     */
    public static String formatDate() {
        String week = "";
        String time = "";
        Date date = new Date();
        switch (date.getDay()) {
            case 0:
                week = "星期日";
                break;
            case 1:
                week = "星期一";
                break;
            case 2:
                week = "星期二";
                break;
            case 3:
                week = "星期三";
                break;
            case 4:
                week = "星期四";
                break;
            case 5:
                week = "星期五";
                break;
            case 6:
                week = "星期六";
                break;
        }
        if (date == null) {
            date = new Date();
        }
        time = (1900 + date.getYear()) + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日" + " " + week;
        return time;
    }

}
